package com.kevin;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

import com.kevin.bean.Project;
import com.kevin.core.Builder;
import com.kevin.util.ConfigUtil;
import com.kevin.util.StringUtils;

public class Main {

	protected Shell shell;
	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
	private Text workspace;
	private Text serviceName;
	private Text operationName;
	private Button btnNewButton;
	private Combo projectType;
	private Combo messageXsdVersion;
	private Combo isNewProject;
	private Combo hasProvider;
	private Label msg;
	static String default_workspace=System.getProperty("exe.path");
	static final Color color = new Color(null, 238,238,238);
	
	static{
		if(StringUtils.isBlank(default_workspace)){
			default_workspace="";
		}
			
	}
	

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Main window = new Main();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(573, 318);
		shell.setText("Easy BussinessWork");
		{
			TableWrapLayout twl_shell = new TableWrapLayout();
			twl_shell.numColumns = 2;
			shell.setLayout(twl_shell);
		}
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setLayoutData(new TableWrapData(TableWrapData.RIGHT, TableWrapData.TOP, 1, 1));
		formToolkit.adapt(lblNewLabel, true, true);
		lblNewLabel.setText("Workspace：");
		lblNewLabel.setBackground(color);
		
		workspace = new Text(shell, SWT.BORDER);
		workspace.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		formToolkit.adapt(workspace, true, true);
		workspace.setText(ConfigUtil.get("workspace",default_workspace));
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setLayoutData(new TableWrapData(TableWrapData.RIGHT, TableWrapData.TOP, 1, 1));
		label_1.setText("工程类型：");
		formToolkit.adapt(label_1, true, true);
		label_1.setBackground(color);
		
		projectType = new Combo(shell, SWT.READ_ONLY);
		projectType.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		projectType.add("请求应答");
		projectType.setData("0","req-res");
		projectType.add("发布订阅");
		projectType.setData("1","pub-sub");
		formToolkit.adapt(projectType);
		formToolkit.paintBordersFor(projectType);
		
		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setLayoutData(new TableWrapData(TableWrapData.RIGHT, TableWrapData.TOP, 1, 1));
		label_2.setText("消息版本：");
		formToolkit.adapt(label_2, true, true);
		label_2.setBackground(color);
		
		messageXsdVersion = new Combo(shell, SWT.READ_ONLY);
		messageXsdVersion.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		messageXsdVersion.add("v1.0.0");
		messageXsdVersion.setData("0","1.0.0");
		messageXsdVersion.add("v1.1.0");
		messageXsdVersion.setData("1","1.1.0");
		formToolkit.adapt(messageXsdVersion);
		formToolkit.paintBordersFor(messageXsdVersion);
		
		Label label_3 = new Label(shell, SWT.NONE);
		label_3.setLayoutData(new TableWrapData(TableWrapData.RIGHT, TableWrapData.TOP, 1, 1));
		label_3.setText("服务名称：");
		formToolkit.adapt(label_3, true, true);
		label_3.setBackground(color);
		
		serviceName = new Text(shell, SWT.BORDER);
		serviceName.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		formToolkit.adapt(serviceName, true, true);
		
		Label label_4 = new Label(shell, SWT.NONE);
		label_4.setLayoutData(new TableWrapData(TableWrapData.RIGHT, TableWrapData.TOP, 1, 1));
		label_4.setText("操作名称：");
		formToolkit.adapt(label_4, true, true);
		label_4.setBackground(color);
		
		operationName = new Text(shell, SWT.BORDER);
		operationName.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		formToolkit.adapt(operationName, true, true);
		
		Label label_7 = new Label(shell, SWT.NONE);
		label_7.setLayoutData(new TableWrapData(TableWrapData.RIGHT, TableWrapData.TOP, 1, 1));
		label_7.setText("是否新工程：");
		formToolkit.adapt(label_7, true, true);
		label_7.setBackground(color);
		
		isNewProject = new Combo(shell, SWT.READ_ONLY);
		isNewProject.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		isNewProject.add("否");
		isNewProject.setData("0","false");
		isNewProject.add("是");
		isNewProject.setData("1","true");
		formToolkit.adapt(isNewProject);
		formToolkit.paintBordersFor(isNewProject);
		
		Label label_9 = new Label(shell, SWT.NONE);
		label_9.setLayoutData(new TableWrapData(TableWrapData.RIGHT, TableWrapData.TOP, 1, 1));
		label_9.setText("是否有Provider：");
		formToolkit.adapt(label_9, true, true);
		label_9.setBackground(color);
		
		hasProvider = new Combo(shell, SWT.READ_ONLY);
		hasProvider.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		hasProvider.add("否");
		hasProvider.setData("0","false");
		hasProvider.add("是");
		hasProvider.setData("1","true");
		formToolkit.adapt(hasProvider);
		formToolkit.paintBordersFor(hasProvider);
		new Label(shell, SWT.NONE);
		
		btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				super.mouseUp(e);
				try {
					if(validate()){
						Project p=getProject();
						Builder builder = new Builder(p);
						builder.build();
						info("生成成功！");
					}else{
						
					}
				} catch (Exception e2) {
					error("生成失败！"+e2.getMessage());
				}
			}
		});
		formToolkit.adapt(btnNewButton, true, true);
		btnNewButton.setText(" 生成 ");
		new Label(shell, SWT.NONE);
		
		msg = new Label(shell, SWT.NONE);
		msg.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.TOP, 1, 1));
		formToolkit.adapt(msg, true, true);
		msg.setBackground(color);
	}
	
	
	private boolean validate(){
		String _workspace = this.workspace.getText();
		String _serviceName = this.serviceName.getText();
		String _operationName = this.operationName.getText();
		if(StringUtils.isBlank(_workspace)){
			error("[Workspace]不允许为空！");
			return false;
		}else if(StringUtils.isBlank(_serviceName)){
			error("[服务名称]不允许为空！");
			return false;
		}else if(StringUtils.isBlank(_operationName)){
			error("[操作名称]不允许为空！");
			return false;
		}
		return true;
	}
	
	/**
	 * 生成配置
	 * @return
	 */
	private Project getProject(){
		Project p=new Project();
		String _messageXsdVersion = (String)messageXsdVersion.getData(String.valueOf(messageXsdVersion.getSelectionIndex()));
		p.setMessageXsdVersion(_messageXsdVersion);
		
		String _isNewProject=(String)isNewProject.getData(String.valueOf(isNewProject.getSelectionIndex()));
		p.setNewProject(Boolean.parseBoolean(_isNewProject));
		
		String _hasProvider=(String)hasProvider.getData(String.valueOf(hasProvider.getSelectionIndex()));
		p.setHasProvider(Boolean.parseBoolean(_hasProvider));
		
		String _operationName = operationName.getText().trim();
		p.setOperationName(_operationName);
		
		String _projectType = (String)projectType.getData(String.valueOf(projectType.getSelectionIndex()));
		p.setProjectType(_projectType);
		
		String _serviceName = serviceName.getText().trim();
		p.setServiceName(_serviceName);
		
		String _workspace = workspace.getText().trim();
		p.setWorkspace(_workspace);
		
		return p;
	}
	
	private void info(String s){
		msg.setSize(100, 24);
		msg.setText("INFO: "+s);
		msg.setBackground(color);
	}
	
	private void error(String s){
		msg.setSize(445, 20);
		msg.setText("ERROR: "+s);
		msg.setToolTipText(s);
		msg.setBackground(new Color(null, 255,0,0));
	}
	
	

	
	
	public String trace(Exception e) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(stringWriter);
        e.printStackTrace(writer);
        StringBuffer buffer = stringWriter.getBuffer();
        return buffer.toString();
    }
}
